package com.franklin.ideaplugin.easytesting.editor;

import com.franklin.ideaplugin.api.ui.ToolBarWindowForm;
import com.franklin.ideaplugin.easytesting.constant.ExecuteFormType;
import com.franklin.ideaplugin.easytesting.ui.history.ExecuteHistoryFormNew;
import com.franklin.ideaplugin.easytesting.vfs.ExecuteMethodVirtualFile;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorLocation;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiMethod;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.util.Objects;

/**
 * @author Franklin
 * @since 2023/07/30 09:05
 */
public class ExecuteDubboMethodFormEditorProvider extends BaseExecuteMethodEditorProvider {

    @Override
    protected boolean accept(@NotNull Project project, @NotNull ExecuteMethodVirtualFile executeMethodVirtualFile) {
        PsiMethod psiMethod = executeMethodVirtualFile.getPsiMethod();
        if (Objects.isNull(psiMethod)){
            return false;
        }
        if (!psiMethod.getContainingClass().isInterface()){
            return false;
        }
        return true;
    }

    @Override
    protected @NotNull FileEditor buildEditor(@NotNull Project project, @NotNull ExecuteMethodVirtualFile executeMethodVirtualFile) {
        PsiMethod interfaceClassName = executeMethodVirtualFile.getPsiMethod();
        ExecuteHistoryFormNew executeMethodResultForm = new ExecuteHistoryFormNew(interfaceClassName, ExecuteFormType.DUBBO);
        ToolBarWindowForm toolBarWindowForm = new ToolBarWindowForm();
        toolBarWindowForm.addContent(executeMethodResultForm.getMainPanel(),new Dimension(1300,800));
        toolBarWindowForm.addToolButton(executeMethodResultForm.getExecuteBtn());
        executeMethodResultForm.init();
        executeMethodVirtualFile.setExecuteHistoryFormNew(executeMethodResultForm);
        return new FileEditor() {

            @Override
            public @Nullable VirtualFile getFile() {
                return executeMethodVirtualFile.getDelegate();
            }

            @Override
            public @NotNull JComponent getComponent() {
                return toolBarWindowForm.getMainPanel();
            }

            @Override
            public @Nullable JComponent getPreferredFocusedComponent() {
                return toolBarWindowForm.getMainPanel();
            }

            @Override
            public @Nls(capitalization = Nls.Capitalization.Title) @NotNull String getName() {
                return "Execute Dubbo Form";
            }

            @Override
            public void setState(@NotNull FileEditorState state) {

            }

            @Override
            public boolean isModified() {
                return false;
            }

            @Override
            public boolean isValid() {
                return true;
            }

            @Override
            public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) {

            }

            @Override
            public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) {

            }

            @Override
            public @Nullable FileEditorLocation getCurrentLocation() {
                return null;
            }

            @Override
            public void dispose() {

            }

            @Override
            public <T> @Nullable T getUserData(@NotNull Key<T> key) {
                return null;
            }

            @Override
            public <T> void putUserData(@NotNull Key<T> key, @Nullable T value) {

            }
        };
    }

    @Override
    public void writeState(@NotNull FileEditorState _state, @NotNull Project project, @NotNull Element element) {

    }
}
